package com.gogowan.agroenterprise.supply.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.impl.client.DefaultHttpClient;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class Net {

	private InputStream inputstream;
	private DefaultHttpClient httpClient;
	private boolean isStop = false;
	/**
	 * 资源下载
	 * 
	 * @param context
	 *            上下文对象
	 * @param url
	 *            下载url地址
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public byte[] downloadResource(Context context, String path) throws Exception {
		/*isStop = false;
		ByteArrayBuffer buffer = null;
		HttpGet hp = new HttpGet(url);
		httpClient = new DefaultHttpClient();
		String netType = isNetType(context);
		if (netType != null & netType.equals("cmwap")) {
			HttpHost proxy = new HttpHost("10.0.0.172", 80);
			httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY,
					proxy);
		}
		HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),
				5 * 1000);
		HttpConnectionParams.setSoTimeout(httpClient.getParams(), 60 * 1000);
		HttpResponse response = httpClient.execute(hp);
		if (response.getStatusLine().getStatusCode() == 200) {
			inputstream = response.getEntity().getContent();
			if (inputstream != null) {
				int i = (int) response.getEntity().getContentLength();
				buffer = new ByteArrayBuffer(1024);
				byte[] tmp = new byte[1024];
				int len;
				while (((len = inputstream.read(tmp)) != -1)
						&& (false == isStop)) {
					buffer.append(tmp, 0, len);
				}
			}
			cancel();
		}
		return buffer.toByteArray();*/
		
		URL url = new URL(path);

		HttpURLConnection conn = (HttpURLConnection) url.openConnection();

		conn.setRequestMethod("GET");

		conn.setConnectTimeout(5 * 1000);

		InputStream inStream = conn.getInputStream();

		return readFromInput(inStream);
		
	}
	
	/**
	 * 强制关闭请求
	 * 
	 * @throws IOException
	 */
	public synchronized void cancel() throws IOException {
		if (null != httpClient) {
			isStop = true;
			httpClient.getConnectionManager().shutdown();
			httpClient = null;
		}
		if (inputstream != null) {
			inputstream.close();
		}
	}
	
	
	/**
	 * 判断接入点类型
	 * 
	 * @return
	 */
	public static String isNetType(Context context) {
		String nettype = null;
		if (context == null) {
			return null;
		}
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo mobNetInfo = connectivityManager.getActiveNetworkInfo();
		if (mobNetInfo != null) {
			if (mobNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
				nettype = mobNetInfo.getTypeName(); // 当前联网类型是WIFI
			} else {
				nettype = mobNetInfo.getExtraInfo();// 当前联网类型是cmnet/cmwap
			}
		}

		return nettype;
	}
	

	public static byte[] readFromInput(InputStream inStream) throws Exception {

		ByteArrayOutputStream outStream = new ByteArrayOutputStream();

		byte[] buffer = new byte[1024];

		int len = 0;

		while ((len = inStream.read(buffer)) != -1) {

			outStream.write(buffer, 0, len);

		}

		inStream.close();

		return outStream.toByteArray();

	}
}
